package org.sjs.dreamfly.browser.jsbridge

import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.json.JSONObject
import org.sjs.dreamfly.app.MovieListLoadedEvent
import org.sjs.dreamfly.app.PlayInfoLoadedEvent
import org.sjs.dreamfly.app.PlayListLoadedEvent
import org.sjs.dreamfly.app.SearchListLoadedEvent
import org.sjs.dreamfly.app.defaultEventBus
import org.sjs.dreamfly.biz.movie.MovieUtil
import org.sjs.dreamfly.browser.WebLocation
import org.sjs.dreamfly.model.MovieInfo
import org.sjs.dreamfly.model.VideoInfo
import org.sjs.dreamfly.common.util.JsonUtil
import org.sjs.dreamfly.common.util.LogUtil
import org.sjs.dreamfly.common.util.PhemonUtils
import org.sjs.dreamfly.model.ixigua.iXiGuaFilterResponse

interface JsCallMethod {

    fun onSaveFile(params: String)
    fun onMovieListLoaded(params: String)
    fun onPlayListLoaded(params: String)
    fun onPlayInfoLoaded(params: String)
    fun onSearchListLoaded(params: String)
}

class JsCallMethodParams<T>(
    val location: WebLocation?,
    var data: T?,
    var dataList: List<T>?
)

object JsCallMethodImpl : JsCallMethod {

    override fun onSaveFile(params: String) {
        val fileName = JSONObject(params).opt("fileName")?.toString()
        JSONObject(params).opt("content")?.toString()?.let {
            MainScope().launch {
                PhemonUtils.saveFile(
                    PhemonUtils.getApplication(),
                    "${PhemonUtils.getApplication().cacheDir.absolutePath}/$fileName"
                    , it)
            }
        }
    }
    override fun onMovieListLoaded(params: String) {
        LogUtil.d("params:$params")
        if (MovieUtil.isIXiGuaFilterResponse(params)) {
            JsonUtil.fromJson<JsCallMethodParams<iXiGuaFilterResponse>>(params)?.let {
                it.data?.let { rep ->
                    defaultEventBus.post(MovieListLoadedEvent(it.location, rep))
                }
            }
        } else {
            JsonUtil.fromJson<JsCallMethodParams<MovieInfo>>(params)?.let {
                it.dataList?.let { rep ->
                    defaultEventBus.post(MovieListLoadedEvent(it.location, rep))
                }
            }
        }
    }

    override fun onPlayListLoaded(params: String) {
        LogUtil.d("params:$params")
        JsonUtil.fromJson<JsCallMethodParams<VideoInfo>>(params)?.let {
            if (it.dataList?.isNotEmpty() == true) {
                defaultEventBus.post(PlayListLoadedEvent(it.location, it.dataList?: emptyList()))
            }
        }
    }

    override fun onPlayInfoLoaded(params: String) {
        LogUtil.d("params:$params")
        try {
            PlayInfoLoadedEvent(VideoInfo(
                videoUrl = JSONObject(params).opt("url")?.toString(),
                title = JSONObject(params).opt("title")?.toString(),
                imageUrl = JSONObject(params).opt("imageUrl")?.toString(),
                refer = JSONObject(params).opt("link")?.toString(),
                nextRefer = JSONObject(params).opt("link_next")?.toString(),
                prevRefer = JSONObject(params).opt("link_pre")?.toString(),
                nextVideoUrl = JSONObject(params).opt("url_next")?.toString(),
                movieId = JSONObject(params).opt("id")?.toString(),
                sid = JSONObject(params).opt("sid")?.toString()?.toInt()?:1,
                nid = JSONObject(params).opt("nid")?.toString()?.toInt()?:1,
                encrypt = JSONObject(params).opt("encrypt")?.toString()?.toInt()?:0)).apply {
                defaultEventBus.post(this)
            }

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun onSearchListLoaded(params: String) {
        LogUtil.d("params:$params")
        JsonUtil.fromJson<MutableList<MovieInfo>>(params)?.let {
            if (it.isNotEmpty()) {
                defaultEventBus.post(SearchListLoadedEvent(it))
            }
        }
    }
}